package org.glut.cloud.materialManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.glut.cloud.materialManage.entity.MaterialChange;
import org.glut.cloud.materialManage.entity.MaterialsManage;
import org.glut.cloud.materialManage.mapper.MaterialChangeMapper;
import org.glut.cloud.materialManage.mapper.MaterialsManageMapper;
import org.glut.cloud.materialManage.service.IMaterialsManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 材料管理表 服务实现类
 *
 * @author sanminzhi
 * @since 2024-07-19 11:26
 */
@Service
@Slf4j
public class MaterialsManageServiceImpl extends ServiceImpl<MaterialsManageMapper, MaterialsManage> implements IMaterialsManageService {

    @Autowired
    MaterialsManageMapper materialsManageMapper;

    @Autowired
    MaterialChangeMapper materialChangeMapper;


    @Override
    public List<MaterialsManage> selectAll() {
        return list();
    }

    @Override
    public Page<MaterialsManage> selectPage(int page, int size, String category, String query) {
        QueryWrapper<MaterialsManage> queryWrapper = new QueryWrapper<>();
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq("category", category);
        }
        if (query != null && !query.isEmpty()) {
            queryWrapper.like("material_name", query)
                    .or().like("material_description", query);
        }
        return page(new Page<>(page, size), queryWrapper);
    }


    @Override
    public List<MaterialsManage> getMaterialsManage(){
        LambdaQueryWrapper<MaterialsManage> materialsManage = new LambdaQueryWrapper<>();
        return this.list(materialsManage);
    }

    @Override
    public List<MaterialsManage> findMaterials(String materialName) {

        LambdaQueryWrapper<MaterialsManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialsManage::getMaterialName,materialName);

        // 执行查询，获取符合条件的所有结果
        List<MaterialsManage> materialsManages = list(queryWrapper);
        log.info("查询{}",materialName);
        log.info("查询{}",materialsManages);
        return materialsManages;
    }

    @Override
    public List<MaterialsManage> getLowStockMaterials(int threshold) {
        LambdaQueryWrapper<MaterialsManage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(MaterialsManage::getQuantity, threshold);
        return list(queryWrapper);
    }

    @Override
    public boolean deductMaterial(Integer materialId, int quantity) {
        MaterialsManage material = getById(materialId);
        if (material == null) {
            log.error("材料编号{}对应的物料不存在", materialId);
            return false;
        }

        // 检查库存是否足够
        if (material.getQuantity() < quantity) {
            log.error("材料编号{}对应的物料库存不足", materialId);
            return false;
        }

        // 执行库存扣减
        material.setQuantity(material.getQuantity() - quantity);
        boolean result = updateById(material);
        if (result) {
            log.info("成功扣减材料编号{}的库存，扣减数量：{}", materialId, quantity);
            // 记录库存变动
            MaterialChange record = new MaterialChange();
            record.setMaterialId(materialId);
            record.setMaterialName(material.getMaterialName());
            record.setChangeQuantity(-quantity); // 扣减为负数
            record.setChangeTime(new Date());
            record.setChangeType("减少");
            materialChangeMapper.insert(record);
        } else {
            log.error("扣减材料编号{}的库存失败", materialId);
        }
        return result;
    }

    @Override
    public Page<MaterialChange> getMaterialChange(int page,int size){
        QueryWrapper<MaterialChange> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("change_time");

        return materialChangeMapper.selectPage(new Page<>(page,size),queryWrapper);
    }
}
